<h1 id="高阶函数">高阶函数</h1>
<p>Quick支持高阶函数，匿名函数。<br>这意味着函数可以储存在变量中，可以作为参数传递，可以作为返回值。极大地提高了程序灵活性。</p>
<p>在js中用关键字<code>function</code>或<code>()=&gt;</code>标识一个函数<br>golang中用关键字<code>func</code>标识一个函数<br>Quick用符号<code>$</code>来标识一个匿名函数，非匿名函数无需标识</p>
<pre><code>// way 1
$ [args...] -&gt; result
// way 2
$ [args...] {}
// way 3
$ expression
</code></pre>
<p>当匿名函数只有一个返回语句时，可以<code>-&gt;</code>来表示一个<code>return</code>标识符，不写<code>{}</code></p>
<pre><code class="hljs language-javascript">upper = $ str -&gt; str.<span class="hljs-title function_">upper</span>()
<span class="hljs-title function_">println</span>(<span class="hljs-title function_">upper</span>(<span class="hljs-string">&quot;hello&quot;</span>)) <span class="hljs-comment">// HELLO</span>
</code></pre>
<p>当匿名函数只有一个表达式语句时且没有形参，直接在前面加一个<code>$</code>标识即可.</p>
<pre><code class="hljs language-javascript">pt = $ <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;It&#x27;s simple function!!!&quot;</span>)
<span class="hljs-title function_">pt</span>() <span class="hljs-comment">// It&#x27;s simple function!!!</span>
</code></pre>
<p>示例一：</p>
<pre><code class="hljs language-javascript"><span class="hljs-title function_">service</span>(<span class="hljs-params">name, fn</span>) {
    <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;fn is&quot;</span>, fn) <span class="hljs-comment">// fn is func:()</span>
    <span class="hljs-title function_">printf</span>(<span class="hljs-string">&quot;%v =&gt; %v&quot;</span>, name, <span class="hljs-title function_">fn</span>(name)) <span class="hljs-comment">// tom =&gt; tom is a cat!</span>
}

<span class="hljs-title function_">service</span>(<span class="hljs-string">&quot;tom&quot;</span>, $ name {
    <span class="hljs-keyword">return</span> name + <span class="hljs-string">&quot; is a cat!&quot;</span>
})
</code></pre>
<p>示例二：</p>
<pre><code class="hljs language-javascript"><span class="hljs-title function_">test</span>(<span class="hljs-params">a, b, add</span>) {
    <span class="hljs-title function_">printf</span>(<span class="hljs-string">&quot;%v + %v = %v\n&quot;</span>, a, b, <span class="hljs-title function_">add</span>(a, b)) <span class="hljs-comment">// 5 + 7 = 12</span>
}

<span class="hljs-title function_">test</span>(<span class="hljs-number">5</span>, <span class="hljs-number">7</span>, $ num1, num2 -&gt; num1 + num2)
</code></pre>
